package com.nip.workmanager.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.nip.workmanager.common.ConstantDatabase;
import com.nip.workmanager.common.utilities.UtilitiesDataBase;
import com.nip.workmanager.common.utilities.UtilitiesObjectId;
import com.nip.workmanager.entity.CheckListEntity;

import android.R.bool;
import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CheckListTable extends SQLiteOpenHelper {

	private Context _mContext;

	public CheckListTable(Context context) {
		super(context, ConstantDatabase.DATABASE_NAME, null,
				ConstantDatabase.DATABASE_VERSION);
		_mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
	}

	/**
	 * Get biggestId of checklist
	 * 
	 * @return biggestId of checklist
	 */
	private String GetBiggestId() {
		String strQuery = "SELECT * FROM "
				+ ConstantDatabase.CHECKLIST_TABLE_NAME + " ORDER BY ( "
				+ ConstantDatabase.WORK_ID + " ) DESC";
		SQLiteDatabase sQLiteDatabase = getReadableDatabase();
		Cursor cursor = sQLiteDatabase.rawQuery(strQuery, null);
		if (cursor.moveToFirst()) {
			String biggestId = cursor.getString(cursor
					.getColumnIndex(ConstantDatabase.WORK_ID));
			return biggestId;
		}
		sQLiteDatabase.close();
		return null;

	}

	/**
	 * Insert a work to checklist table
	 * 
	 * @param checkListEntity
	 *            object insert to database
	 * @return True/False Success/Error
	 */
	public Boolean InsertWorkToCheckList(CheckListEntity checkListEntity) {
		boolean result = false;
		try {
			String biggestId = GetBiggestId();
			String workId = UtilitiesObjectId.MakeObjecId(biggestId,
					ConstantDatabase.ID_EXTENSION, ConstantDatabase.ID_LENGHT);
			SQLiteDatabase sQLiteDatabase = getWritableDatabase();
			ContentValues contentValues = new ContentValues();
			contentValues.put(ConstantDatabase.WORK_ID, workId);
			contentValues.put(ConstantDatabase.WORK_NAME,
					checkListEntity.getWorkName());
			contentValues.put(ConstantDatabase.ICON, checkListEntity.getIcon());
			contentValues.put(ConstantDatabase.DESCRIPTION,
					checkListEntity.getDescription());
			sQLiteDatabase.insert(ConstantDatabase.CHECKLIST_TABLE_NAME, null,
					contentValues);
			sQLiteDatabase.close();
			result = true;

		} catch (Exception e) {
			// TODO: handle exception
			result = false;
		}

		return result;
	}

	/**
	 * Delete a work in check list
	 * 
	 * @param id
	 *            Work id
	 * @return
	 */
	public Boolean DeleteWorkInCheckListById(String id) {
		SQLiteDatabase wdb = getWritableDatabase();
		try {

			wdb.delete(ConstantDatabase.CHECKLIST_TABLE_NAME,
					ConstantDatabase.WORK_ID + "=?", new String[] { id });
			wdb.close();
			return true;

		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}

	}

	public Boolean UpdateWorkInCheckListById(CheckListEntity checkListEntity) {
		SQLiteDatabase wdb = getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put(ConstantDatabase.WORK_NAME,
					checkListEntity.getWorkName());
			values.put(ConstantDatabase.DESCRIPTION,
					checkListEntity.getDescription());
			values.put(ConstantDatabase.ICON, checkListEntity.getIcon());
			wdb.update(ConstantDatabase.CHECKLIST_TABLE_NAME, values,
					ConstantDatabase.WORK_ID + "=?",
					new String[] { checkListEntity.getWorkId() });
			wdb.close();
			return true;

		} catch (Exception e) {
			// TODO: handle exception
			Log.e("UpdateWorkInCheckListById", e.getMessage());
			return false;
		}
	}

	/**
	 * Get all list the work in checklist
	 * 
	 * @return ArrayList<CheckListEntity list work
	 */
	public ArrayList<CheckListEntity> GetAllWorkInCheckList() {
		SQLiteDatabase rdb = getReadableDatabase();
		String strQuery = "SELECT * FROM "
				+ ConstantDatabase.CHECKLIST_TABLE_NAME;
		Cursor dataTable = rdb.rawQuery(strQuery, null);

		if (dataTable.moveToFirst()) {
			ArrayList<CheckListEntity> listResponse = new ArrayList<CheckListEntity>();
			CheckListEntity workEntity;
			do {
				workEntity = new CheckListEntity();

				workEntity.setWorkId(dataTable.getString(dataTable
						.getColumnIndex(ConstantDatabase.WORK_ID)));
				workEntity.setWorkName(dataTable.getString(dataTable
						.getColumnIndex(ConstantDatabase.WORK_NAME)));
				workEntity.setIcon(Integer.parseInt(dataTable
						.getString(dataTable
								.getColumnIndex(ConstantDatabase.ICON))));
				workEntity.setDescription(dataTable.getString(dataTable
						.getColumnIndex(ConstantDatabase.DESCRIPTION)));

				listResponse.add(workEntity);
			} while (dataTable.moveToNext());
			return listResponse;
		}
		return null;

	}
}
